package com.yixing.tech.wms.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BaseResponse;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.wms.param.ProductCallEmptyParam;
import com.yixing.tech.wms.service.LineOperationService;
import com.yixing.tech.wms.stock.out.callback.error.TaskErrorCallbackVO;
import com.yixing.tech.common.entity.WCSTaskCallbackVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 产线操作
 *
 * @author yixing tech
 * @since 1.0.0
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/line/opt")
public class LineOperationController extends BaseControl {

    @Autowired
    private LineOperationService lineOperationService;

    @GetMapping("/product/locations")
    public Rs<Object> productLocations() throws BusinessException {
        return Rs.ok(lineOperationService.productLocations());
    }

    @PostMapping("/product/call/empty")
    public Rs<Object> productCallEmpty(@RequestBody ProductCallEmptyParam param) throws BusinessException {
        return Rs.ok(lineOperationService.productCallEmpty(param.getMaterialCode(), param.getEndLocationCode()));
    }

    @PostMapping("/product/call/empty/callback")
    public Rs<Object> productCallEmptyCallback(@RequestBody WCSTaskCallbackVO callbackVO) throws BusinessException {
        log.info("产线呼叫空框 callback, 请求参数:{}", callbackVO);

        lineOperationService.productCallEmptyCallback(callbackVO.getTaskCode(), callbackVO.getMethod(),
                callbackVO.getCurrentPositionCode(), callbackVO.getPodCode());

        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc("任务节点反馈接收成功!");
        return response;
    }

    @PostMapping("/product/call/empty/error/callback")
    public Rs<Object> productCallEmptyErrorCallback(@RequestBody TaskErrorCallbackVO callbackVO) throws BusinessException {
        log.error("产线呼叫空框 errorCallback, 请求参数:{}", callbackVO);

        Object data = lineOperationService.productCallEmptyErrorCallback(callbackVO);
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setData(data);
        response.setDesc("任务节点反馈接收成功!");
        return response;
    }

    @GetMapping("/raw/locations")
    public Rs<Object> rawLocations() throws BusinessException {
        return Rs.ok(lineOperationService.rawLocations());
    }

}
